<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function _initialize()
    {
        parent::_initialize();

        $banner = Db::name('banner')->order('id desc')->where([
            'lanmu' => 'index'
        ])->select();
        $this->assign('banner', $banner);
    }

    //首页
    public function index()
    {
        $product = Db::name('product')->where([
            'hot_switch' => 1
        ])->limit(0,3)->order('weigh desc,id desc')->select();
        $this->assign('product', $product);

        $process = Db::name('process')->order('weigh desc,id desc')->select();
        $this->assign('process', $process);

        $idea = Db::name('idea')->order('weigh desc,id desc')->select();
        $this->assign('idea', $idea);

        $art = Db::name('art')->order('weigh desc,id desc')->select();
        $this->assign('art', $art);

        $service = Db::name('service')->order('weigh desc,id desc')->select();
        $this->assign('service', $service);

        $discover = Db::name('discover')->order('weigh desc,id desc')->select();
        $this->assign('discover', $discover);

        $brand = Db::name('brand')->order('weigh desc,id desc')->select();
        $this->assign('brand', $brand);

        $join_support = Db::name('join_support')->order('weigh desc,id desc')->select();
        $this->assign('join_support', $join_support);

        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        $is_mobile = false;
        if (preg_match('/(iphone|ipod|android|mobile|windows phone)/i', $userAgent)) {
            $is_mobile = true;
        }
        $vfx = Db::name('vfx')->order('weigh desc,id desc')->select();
        foreach ($vfx as $k => &$v) {
            if($is_mobile){
                $v['image'] = $v['mb_image'];
            }else{
                $v['image'] = $v['pc_image'];
            }
        }
        $this->assign('vfx', $vfx);

        return $this->view->fetch();
    }


    //留言
    public function message()
    {
        $params = $this->request->param();
        if(!$params['addr']){
            $this->success('请输入所在地区');
        }
        if(!$params['product']){
            $this->success('请选择产品类型');
        }
        if(!$params['username']){
            $this->success('请输入联系人');
        }
        if(!$params['telephone']){
            $this->success('请输入手机号');
        }

        $insert = [];
        $insert['telephone'] = xykeepFilterSql($params['telephone']);
        $insert['username'] = xykeepFilterSql($params['username']);
        $insert['addr'] = xykeepFilterSql($params['addr']);
        $insert['product'] = xykeepFilterSql($params['product']);
        $insert['createtime'] = time();
        Db::name('message')->insert($insert);
        $this->success('感谢您的留言。');
    }
}
